package org.practice.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.practice.dao.DaoFactory;
import org.practice.dao.OrderDao;
import org.practice.dao.UserDao;
import org.practice.model.Address;
import org.practice.model.Item;
import org.practice.model.Order;
import org.practice.service.CartService;
import org.practice.service.bo.CartBO;
import org.practice.service.bo.CartItemBO;
import org.practice.service.impl.CartServiceImpl;
import org.practice.util.Constant;

public class OrderAction extends CartMappingDispatchAction{
	public ActionForward recipient(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("success");
	}

	public ActionForward confirmOrder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("success");
	}
	public ActionForward showRecipient(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
					throws Exception {
		return mapping.findForward("success");
	}
	public ActionForward confirmOrderAddress(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		Address address = new Address();
		BeanUtils.copyProperties(address, form);
		request.getSession().setAttribute("address", address);
		return mapping.findForward("success");
	}
	
	
	
	public ActionForward createOrder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		CartBO cart = (CartBO) session.getAttribute(Constant.SESSION_KEY_CART);
		Address address = (Address) session.getAttribute("address");
		if (cart == null||address==null) {
			return mapping.findForward("fail");
		}

		OrderDao orderDao = DaoFactory.createOrderDao();
		Order order = new Order();
		List<CartItemBO> cartItems = cart.getEffectiveCartItems();
		Set<Item> items = new HashSet<Item>();
		for (int i = 0; i < cartItems.size(); i++) {
			Item item = new Item();
			CartItemBO ct = (CartItemBO) cartItems.get(i);
			item.setProduct(ct.getProduct());
			item.setAmount(ct.getProduct().getDangPrice() * ct.getNumber());
			item.setDangPrice(ct.getProduct().getDangPrice());
			item.setNum(ct.getNumber());
			items.add(item);
		}

		order.setItems(items);
		
		order.setAddress(address);
		
		UserDao userDao = DaoFactory.createUserDao();
		order.setUser(userDao.findUserById(cart.getUserId()));
		
		order.setOrderTime(System.currentTimeMillis());
	
		order.setOrderPrice(cart.getTotalPrice());
		order.setTotalPrice(order.getOrderPrice()+0);

		order.setStatus(1);
		
		orderDao.addOrder(order);
		
		session.setAttribute("order", order);
		CartService service=new CartServiceImpl();
		service.clear(cart);
		return mapping.findForward("success");
	}
}
